import { HttpContext } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
import { ALLOW_ANONYMOUS } from '@delon/auth';
import { SFSchema, SFUISchema, SFSelectWidgetSchema, SFRadioWidgetSchema } from '@delon/form';
import {_HttpClient, SettingsService} from '@delon/theme';
import { NzMessageService } from 'ng-zorro-antd/message';
import { NzModalRef } from 'ng-zorro-antd/modal';
import { Api } from 'src/app/shared/api/api';
import { USER_LABEL_ENUM, DELETED_STATUS_ENUM, VCATEGORIES, XS_SOURCE, SIX } from 'src/app/shared/constant/common-constant';
import { ResponseCode } from 'src/app/shared/response.code';

@Component({
  selector: 'app-system-user-edit-contract',
  styleUrls: ['./base.component.less'],
  templateUrl: './user-edit-contract.component.html'
})
export class UserEditContractComponent implements OnInit {
  record: any = {};
  readonly: boolean = false;
  userLabel: any = [];
  userStatus: any = [];
  xs_source_list: any = [];
  six_list: any = [];
  user: "" | undefined;
  schema: SFSchema = { properties: {} };
  ui: SFUISchema = {
    '*': {
      spanLabel: 10,
      spanControl: 14,
      grid: { span: 12 }
    }
  };

  constructor(private modal: NzModalRef,
              private settings: SettingsService,
              private msgSrv: NzMessageService,
              public http: _HttpClient) {}

  ngOnInit(): void {
    this.readonly = this.record.id && this.record.id.length > 0;
    this.userLabel = USER_LABEL_ENUM.filter(item => item.value.length > 0);
    this.userStatus = VCATEGORIES.filter(item => item.value !== null);
    this.xs_source_list = XS_SOURCE.filter(item => item.value !== null);
    this.six_list = SIX.filter(item => item.value !== null);
    this.initSchema();
  }

  // 初始化表单，在获取完部门树数据之后再初始化
  initSchema() {
    this.schema = {
      properties: {
        id: {
          type: 'string',
          title: '客户id',
          readOnly: true
        },
        contract_num: {
          type: 'string',
          title: '合同编号',
          readOnly: this.readonly
        },
        contract_money: {
          type: 'string',
          title: '合同金额'
        },
        contract_receive: {
          type: 'string',
          title: '已收款'
        },
        contract_off_money: {
          type: 'string',
          title: '尾款',
          readOnly: this.readonly
        },
        contract_cost: {
          type: 'string',
          title: '成本',
          readOnly: this.readonly
        },
        contract_time: {
          type: 'string',
          title: '服务开始期限',
          readOnly: this.readonly,
          ui: {
            widget: 'date'
          }
        },
        contract_end_time: {
          type: 'string',
          title: '服务结束期限',
          readOnly: this.readonly,
          ui: {
            widget: 'date'
          }
        }
      }
    };
  }

  save(value: any) {
    console.log(value);
    if (this.record.id) {
      value.contract_url = this.record.contract_url;
      value.contract_pay_url = this.record.contract_pay_url;
      value.customer_id = this.record.id;
      this.http
        .post(Api.BaseCustomerContractUpdateApi, value, {}, { context: new HttpContext().set(ALLOW_ANONYMOUS, true) })
        .subscribe((res: any) => {
          if (res) {
            if (res.code === ResponseCode.SUCCESS) {
              this.msgSrv.success('添加成功');
              this.modal.close(true);
            } else {
              this.msgSrv.warning(res.message);
            }
          } else {
            this.msgSrv.error('添加失败，未知错误');
          }
        });
    }
  }
  close() {
    this.modal.destroy();
  }
  change1($event: any) {
    console.log($event.file.response.data)
    this.record.contract_url = $event.file.response.data
  }
  change2($event: any) {
    console.log($event.file.response.data)
    this.record.contract_pay_url = $event.file.response.data
  }
}
